//
//  HomePageViewController.m
//  ForeverLove
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 文萱竹. All rights reserved.
//

#import "HomePageViewController.h"
#import "MemoryTableViewController.h"

@interface HomePageViewController ()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
//- (IBAction)MemoryButtonDidClicked:(UIButton *)sender;

@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (weak, nonatomic) IBOutlet UIImageView *backGroundImage;



- (IBAction)changeBackgroundImage:(UIButton *)sender;

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置tabBar颜色
    self.tabBarController.tabBar.tintColor = [UIColor colorWithRed:178.0/256.0 green:23.0/256.0 blue:137.0/256.0 alpha:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


//- (IBAction)MemoryButtonDidClicked:(UIButton *)sender {
////    UIStoryboard* bord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
////    MemoryTableViewController* mtc = [bord instantiateViewControllerWithIdentifier:@"memory"];
////    [self presentViewController:mtc animated:YES completion:nil];
//    
//}
#pragma mark --- 点击更改图片按钮, 更换背景图片
- (IBAction)changeBackgroundImage:(UIButton *)sender {
    //选择图片来源
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选取图片" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self setImageFrom:UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self setImageFrom:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void) setImageFrom:(UIImagePickerControllerSourceType)source{
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    _imagePicker.sourceType = source;
    [_imagePicker setEditing:YES];
    _imagePicker.allowsEditing = YES;
    [self presentViewController:_imagePicker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  
    NSString *documentStr = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)[0];
    
    NSString *path = [documentStr stringByAppendingString:@"/backImage.png"];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
   
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    
    [data writeToFile:path atomically:YES];

    
    NSLog(@"documentStr === %@", documentStr);
    NSLog(@"path === %@", path);
    
    self.backGroundImage.image = [UIImage imageWithData:data];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

@end
